<template>
  <div class="dialog">
    <el-dialog :title="'批次' + titleText" :visible.sync="showDialog" width="55%" top="0" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" label-position="left" :model="formData" :rules="rules" class="el-form">
        <el-form-item label="主题名称:" prop="mnecode" class="w50p">
          <el-input v-model="formData.mnecode" clearable />
        </el-form-item>
        <el-form-item label="主题所在地址:" prop="mnecode" class="w50p">
          <el-input v-model="formData.mnecode" clearable />
        </el-form-item>
        <el-form-item label="上传图片:" prop="mnecode" class="w50p">
          <el-input v-model="formData.mnecode" clearable />
        </el-form-item>
        <el-form-item label="备注:" prop="mnecode" class="w50p">
          <el-input v-model="formData.mnecode" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import FishVarietyApi from "@/api/basic-info-manage/fishVariety"
import FishPondApi from "@/api/basic-info-manage/fishPond"
import BatchMangeApi from "@/api/batch-mange"
import BreedingTypeManageApi from "@/api/basic-info-manage/breeding-type-manage/index";
import ConfigCodeApi from "@/api/basic-info-manage/config-code";
import homeBaseApi from "@/api/basic-info-manage/homeBase";
import MaterialTypeApi from "@/api/basic-info-manage/material";
import MaterialTypeManageApi from "@/api/basic-info-manage/material-type-manage"

export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data () {
    let checkInt = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value > 0 && !(value > 100)) {
          callback();
        } else {
          return callback(new Error("请输入大于0小于100的参数"));
        }
      }
    };
    return {
      deadMaterial: {},
      secondaryMaterial: {},
      liveMaterial: {},
      rules: {
        mnecode: [
          { required: true, message: "请输入批次编号", trigger: ["change", "blur"] }
        ],
        productionBatchCode: [
          { required: true, message: "请输入批次编码", trigger: ["change", "blur"] }
        ],
        pondRatio: [
          { required: true, message: "请输入批次鱼塘占比", trigger: ["change", "blur"] }, {
            validator: checkInt, trigger: ["change", "blur"]
          }
        ],
        fishPondId: [
          { required: true, message: "请选择批次所在鱼塘", trigger: ["change", "blur"] }
        ],
        fishVarietyId: [
          { required: true, message: "请选择养殖品种", trigger: ["change", "blur"] }
        ],
        breedType: [
          { required: true, message: "请选择养殖方式", trigger: ["change", "blur"] }
        ],
        clearCompleteFlag: [
          { required: true, message: "请选择批次完成方式", trigger: ["change", "blur"] },
        ],
        breedTypeNo: [
          { required: true, message: "请选择养殖类型", trigger: ["change", "blur"] }
        ],
      },
      formData: {
      },
      //养殖类型
      clearCompleteFlag: [
        {
          typeName: "是",
          typeCode: true,
        },
        {
          typeName: "否",
          typeCode: false,
        },
      ],//是否选项
      useableRate: undefined,
      breedTypeNoList: [],
      fishPondList: [],//鱼塘
      FishVarietyData: [],//品种
      showDialog: this.show,
      materialList: [],
      MaterialTypeManageList: [],
      materialTypeNo: 'FISH',
      varietyName: ''
    };
  },
  computed: {
  },
  watch: {
    show (v) {
      this.showDialog = v;
      if (!v) {
        this.$refs['ruleForm'].resetFields()
        this.varietyName = null
        console.log('重置表单验证')
      } else {
        this.queryCondition()
        this.queryFishVariety()
        this.queryFishPond()
      }
    },
    nowChanceStau (v) {
      if (v === 'add') {
        homeBaseApi.getBreedTypeNo().then(res => {
          this.formData = {
            staffName: sessionStorage.getItem('staffName'),
            breedTypeNo: res.data
          }
        })
        this.generateNo()
      }
      if (v === 'edit') {
        this.formData = this.editObject
        if (!this.formData.productionBatchCode) {
          this.generateNo()
        }
      }
    }
  },
  created () {
  },
  mounted () {
    this.getMaterialTypeManage()
    this.getMaterialType()
  },
  methods: {
    handleChangeFishName (e) {
      console.log(e)
      let fishObj = this.FishVarietyData.filter(function (item) { return item.fishVarietyId == e })
      this.varietyName = fishObj[0].varietyName
    },
    getMaterialTypeManage () {
      MaterialTypeManageApi.queryByCondition().then(res => {
        if (res.data) {
          this.MaterialTypeManageList = res.data
          console.log(this.MaterialTypeManageList, 'MaterialTypeManageList')
        } else {
          this.MaterialTypeManageList = []
        }
      })
    },
    getMaterialType () {
      let params = {
        entity: { materialTypeNo: this.materialTypeNo }
      }
      MaterialTypeApi.queryPageByCondition(params).then(res => {
        if (res.data && res.data.content) {
          this.materialList = res.data.content
          console.log(this.materialList)
        } else {
          this.materialList = []
        }
      })
    },
    generateNo () {
      ConfigCodeApi.generateNo({ noType: 'productionBatch' }).then(res => {
        this.$set(this.formData, 'productionBatchCode', res.data)
      })
    },
    queryCondition () {
      BreedingTypeManageApi.queryByCondition().then(res => {
        if (res.success === true) {
          this.breedTypeNoList = res.data || [];
        }
      });
    },
    pondChange (e) {
      this.fishPondList.find(item => {
        if (item.fishPondId === e) {
          this.useableRate = 100 - item.useRatio;
          return true
        }
      });
    },
    //查询可选鱼类品种
    queryFishVariety () {
      FishVarietyApi.queryByCondition().then(res => {
        this.FishVarietyData = res.data;
      })
    },
    //查询可选鱼塘
    queryFishPond () {
      FishPondApi.queryByCondition().then(res => {
        this.fishPondList = res.data;
      })
    },
    save (formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            BatchMangeApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            BatchMangeApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close () {
      this.queryFishPond()
      this.useableRate = undefined
      this.$emit("closeDialog");
    },
    cancel () {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
